using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;

public partial class admin_news_edit : SiteAdminBasePage
{
    public int _newsId;
    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;
        if (!IsPostBack)
        {
            LoadData();
            InitControls();
        }
    }

    private void LoadData()
    {
        int newsId = 0;
        if (!string.IsNullOrEmpty(Request.QueryString["nid"]) &&
            int.TryParse(Request.QueryString["nid"], out newsId))
        {
            News news = News.Load(newsId);
            this.BindData(news);
            _newsId = newsId;
        }

    }
    protected void Update_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
       
            if (Page.IsValid)
            {

                int newsId = 0;
                if (!string.IsNullOrEmpty(Request.QueryString["nid"]) &&
                    int.TryParse(Request.QueryString["nid"], out newsId))
                {
                    News news = News.Load(newsId);

                    news.NewsName = tbxNewsName.Text;
                    news.NewsBriefDescription = fckNewsBriefDescription.Value;
                    news.NewsDescription = fckNewsDescription.Value;
                    DateTime newsDate;
                    DateTime.TryParse(tbxNewsDate.Text, out newsDate);
                    news.NewsDate = newsDate;
                    news.DisplayOrder = tbxDisplayOrder.Text;
                    news.IsActive = cbxIsActive.Checked;
                    news.UpdateDate = DateTime.Now;
                    news.Update();
                }
            }
        }
        catch
        {
            uccResponseMessage.SetFailMessage("Failed to update the News");
            uccResponseMessage.Visible = true;
        }
    }

    private void BindData(News news)
    {
        if (news != null)
        {
            tbxNewsName.Text = news.NewsName;
            fckNewsBriefDescription.Value = Server.HtmlDecode(news.NewsBriefDescription);
            fckNewsDescription.Value = Server.HtmlDecode(news.NewsDescription);
            tbxNewsDate.Text = news.NewsDate.ToString("MM/dd/yyyy");
            tbxDisplayOrder.Text = news.DisplayOrder;
            cbxIsActive.Checked = news.IsActive;

            grvRelated.DataSource = news.RelatedNews;
            grvRelated.DataBind();



            if (news.RelatedNews != null && news.RelatedNews.Count > 0)
            {
                imbRemove.Visible = true;
            }
            else
            {
                imbRemove.Visible = false;
            }

            ddlRelatedList.DataSource = News.GetNews(99999);
            ddlRelatedList.DataTextField = "NewsName";
            ddlRelatedList.DataValueField = "NewsId";
            ddlRelatedList.DataBind();
        }

    }

    private void InitControls()
    {
        imbRemove.Attributes.Add("onclick", "javascript:return confirm('Are you sure that you want to delete these items?')");
    }

    protected void Add_Related_Items(object sender, EventArgs e)
    {
        try
        {

            int newsId = 0;
            int relatedNewsId = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["nid"]) &&
                int.TryParse(Request.QueryString["nid"], out newsId) &&
                int.TryParse(ddlRelatedList.SelectedItem.Value, out relatedNewsId))
            {
                News news = News.Load(newsId);
                News related = News.Load(relatedNewsId);
                news.AddRelatedNews(related);
                BindData(news);
            }
        }
        catch
        {
            uccResponseMessage.SetFailMessage("Failed to Add Related Article");
            uccResponseMessage.Visible = true;
        }
    }
    protected void Delete_Related_Items(object sender, ImageClickEventArgs e)
    {
        try
        {
            int newsId = 0;
            int.TryParse(Request.QueryString["nid"], out newsId);
            News news = News.Load(newsId);
            if (news != null)
            {
                foreach (GridViewRow row in grvRelated.Rows)
                {
                    CheckBox cbxRelated = (CheckBox)row.FindControl("cbxSelect");
                    if (cbxRelated.Checked)
                    {
                        HiddenField hdfRelatedId = (HiddenField)row.FindControl("hdfRelatedId");
                        int relatedNewsId = 0;
                        if (int.TryParse(hdfRelatedId.Value, out relatedNewsId))
                        {
                            news.DeleteRelatedNews(relatedNewsId);
                        }
                    }
                }
                BindData(news);
            }
        }
        catch
        {
            uccResponseMessage.SetFailMessage("Failed to Add Related Article");
            uccResponseMessage.Visible = true;
        }
    }
    protected void Related_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            CheckBox cbxSelectAll = (CheckBox)e.Row.FindControl("cbxSelectAll");
            cbxSelectAll.Attributes.Add("onclick", "javascript:SelectAll('" + grvRelated.ClientID + "','" + cbxSelectAll.ClientID + "','cbxSelect')");
        }
    }
}
